package com.dartsstats.datasource;

import com.dartsstats.GameAtBat;
import com.dartsstats.DM.GameAtBatDM;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class GameAtBatDataSource {
	
	private SQLiteDatabase database;
	private GameAtBatDM gameAtBatDM;
	private String[] allColumns = { GameAtBatDM.COLUMN_ID, GameAtBatDM.COLUMN_PLAYER,  GameAtBatDM.COLUMN_GAME, GameAtBatDM.COLUMN_INNING, GameAtBatDM.COLUMN_AT_BAT_NUMBER, GameAtBatDM.COLUMN_RESULT, GameAtBatDM.COLUMN_SCORED, GameAtBatDM.COLUMN_RBIS, GameAtBatDM.COLUMN_STOLEN_BASES, GameAtBatDM.COLUMN_BALKS};
	
	public GameAtBatDataSource(Context context) {
		gameAtBatDM = new GameAtBatDM(context);
	}
	
	public void open() throws SQLException {
		database = gameAtBatDM.getWritableDatabase();
	}
	
	public void close() {
		gameAtBatDM.close();
	}
	
	public GameAtBat createGame(int player, int game, int inning, int atBatNumber){
		openDatabaseIfNull();
		ContentValues values = new ContentValues();
		values.put(GameAtBatDM.COLUMN_PLAYER, player);
		values.put(GameAtBatDM.COLUMN_GAME, game);
		values.put(GameAtBatDM.COLUMN_INNING, inning);
		values.put(GameAtBatDM.COLUMN_AT_BAT_NUMBER, atBatNumber);
		long insertId = database.insert(GameAtBatDM.TABLE_GAME_AT_BAT, null, values);
		Cursor cursor = database.query(GameAtBatDM.TABLE_GAME_AT_BAT, allColumns, GameAtBatDM.COLUMN_ID + " = " + insertId, null, null, null, null);
		cursor.moveToFirst();
		GameAtBat gameAtBat = cursorToGameAtBat(cursor);
		cursor.close();
		return gameAtBat;
	}
	
	public void updateGameAtBat(GameAtBat gameAtBat) {
		openDatabaseIfNull();
		ContentValues values = new ContentValues();
		values.put(GameAtBatDM.COLUMN_RESULT, gameAtBat.getResult().getID());
		values.put(GameAtBatDM.COLUMN_SCORED, gameAtBat.getScored());
		values.put(GameAtBatDM.COLUMN_RBIS, gameAtBat.getRbis());
		values.put(GameAtBatDM.COLUMN_STOLEN_BASES, gameAtBat.getStolenBases());
		values.put(GameAtBatDM.COLUMN_BALKS, gameAtBat.getBalks());
		String[] whereArgs = new String[] {String.valueOf(gameAtBat.getId())};
		database.update(GameAtBatDM.TABLE_GAME_AT_BAT, values, GameAtBatDM.COLUMN_ID + " = ?", whereArgs);
	}
	
	public void deleteGameAtBat(GameAtBat gameAtBat) {
		openDatabaseIfNull();
		long id = gameAtBat.getId();
		System.out.println("GameAtBat deleted with id: " + id);
		database.delete(GameAtBatDM.TABLE_GAME_AT_BAT, GameAtBatDM.COLUMN_ID + " = " + id, null);
	}
	
	private GameAtBat cursorToGameAtBat(Cursor cursor) {
		GameAtBat gameAtBat = new GameAtBat();
		gameAtBat.setId(cursor.getLong(0));
		gameAtBat.setPlayer(cursor.getInt(1));
		gameAtBat.setGame(cursor.getInt(2));
		gameAtBat.setInning(cursor.getInt(3));
		gameAtBat.setAtBatNumber(cursor.getInt(4));
		return gameAtBat;
	}
	
	private void openDatabaseIfNull(){
		if(database==null){
			open();
		}
	}

}
